package com.e3.basdoc.measure.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.measure.client.MeasureUnitRPCService;
import com.e3.basdoc.measure.model.MeasureUnitModel;
import com.e3.basdoc.measure.model.MeasureUnitQC;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class MeasureUnitRPCServiceImpl extends GwtRPCAction implements
		MeasureUnitRPCService {
	
	public MeasureUnitModel newMeasureUnit(final MeasureUnitModel model) throws WebException {
		return new GwtRPCExecutor<MeasureUnitModel>() {
			@Override
			public MeasureUnitModel execute() throws ServiceException, Exception {
				return MeasureUnitServiceClient.getMeasureUnitService().newMeasureUnit(model);
			}
		}.doExecute();
	}

	public MeasureUnitModel modifyMeasureUnit(final MeasureUnitModel model) throws WebException {
		return new GwtRPCExecutor<MeasureUnitModel>() {
			@Override
			public MeasureUnitModel execute() throws ServiceException, Exception {
				return MeasureUnitServiceClient.getMeasureUnitService().modifyMeasureUnit(model);
			}
		}.doExecute();
	}

	public List<MeasureUnitModel> getMeasureUnitByCondition(final MeasureUnitQC measureQC) throws WebException {
		return new GwtRPCExecutor<List<MeasureUnitModel>>() {
			@Override
			public List<MeasureUnitModel> execute() throws ServiceException, Exception {
				return MeasureUnitServiceClient.getMeasureUnitQueryService().getMeasureUnitByCondition(measureQC);
			}
		}.doExecute();
	}

	public void deleteMeasureUnits(List<MeasureUnitModel> models) throws WebException {
		if(models == null||models.isEmpty()){
			return;
		}
		final List<Long> ids = new ArrayList<Long>(models.size());
		for (MeasureUnitModel measureModel : models) {
			ids.add(measureModel.getId());
		}
		
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				MeasureUnitServiceClient.getMeasureUnitService().deleteMeasureUnitByIds(ids);
			}
		}.doExecute();
	}

}
